﻿namespace CAION2.ViewModels
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Windows.Data;

    using Classes;
    using Models;

    class SelectStigmaDialogViewModel : ViewModelBase
    {
        private DataRow _selectedStigma;
        private IEnumerable<DataRow> _stigmas;

        public string CurrentCharacterClass { private get; set; }
        public byte CurrentCharacterLevel { private get; set; }
        public bool CurrentCharacterRace { private get; set; }
        
        public ICollectionView StigmasList { get; private set; }

        public DataRow SelectedStigma
        {
            get
            {
                return _selectedStigma;
            }
            set
            {
                if (Equals(_selectedStigma, value)) return;
                _selectedStigma = value;

                OnPropertyChanged("SelectedStigma");
            }
        }

        public void LoadStigmas(List<string> excludeStigmas)
        {
            string fields = "name, translate" + DataModel.EngFix + " AS translate, desc_long" + DataModel.EngFix + " AS desc_long, "
                            + "skillicon_name, level, quality, require_shard, skill_group_name, "
                            + "warrior, scout, mage, cleric, engineer, artist, fighter, knight, assassin, ranger, wizard, elementalist, chanter, priest, gunner, bard, rider, "
                            + "abyss_point, charge_set_name, cost_parameter, cost_end, cost_end_lv, required_sword, required_mace, "
                            + "required_dagger, required_2hsword, required_polearm, required_staff, required_bow, required_orb, required_book, "
                            + "required_gun, required_cannon, required_harp, casting_delay, delay_time, first_target, require_skill1, "
                            + "require_skill2, instant_skill, race_permitted";

            string excludeLine = "";
            if (excludeStigmas != null && excludeStigmas.Any()) excludeLine = " AND skill_group_name NOT IN ('" + string.Join("','", excludeStigmas) + "')";

            string raceLine = "pc_dark";
            if (!CurrentCharacterRace) raceLine = "pc_light";

            string condition = "level <= " + CurrentCharacterLevel
                             + " AND " + ClassGameClass.GetGameClassFieldNameFromClassName(CurrentCharacterClass) + " > 0"
                             + " AND stigma_type IS NULL "
                             + excludeLine
                             + " AND race_permitted LIKE '%" + raceLine + "%' "
                             + " ORDER BY skill_group_name ASC, chain_category_level DESC";

            DataTable stigmasTable = DataModel.GetTable("Stigmas", fields, condition);
            _stigmas = StigmaSet.CreateMaxStigmaListFromTable(stigmasTable);
            
            StigmasList = CollectionViewSource.GetDefaultView(_stigmas);
            
            OnPropertyChanged("StigmasList");
        }

        public void SetSelectedStigma(string itemName)
        {
            if (string.IsNullOrEmpty(itemName)) return;
            SelectedStigma = _stigmas.FirstOrDefault(s => s["name"].ToString() == itemName);
        }
    }
}